package com.czk.tx;

import com.czk.tx.service.MoneyService;
import com.czk.tx.service.RepeatAbleService;
import com.czk.tx.service.SellService;
import com.czk.tx.transactiontemplate.TransactionTemplateService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/11/24 20:24
 */
public class Txtest {

    @Test
    public void test_1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        MoneyService moneyService = applicationContext.getBean("moneyService", MoneyService.class);
        moneyService.traverse();
    }

    @Test
    public void test2() throws IOException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        MoneyService moneyService = applicationContext.getBean("moneyService", MoneyService.class);
        CountDownLatch countDownLatch = new CountDownLatch(100);
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                try {
                    countDownLatch.await();
                    // moneyService.lockTransaction();
                    moneyService.lockTransactionSolution();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }).start();
            countDownLatch.countDown();
        }
        System.in.read();
    }

    /**
     * 测试编程式事务
     */
    @Test
    public void test_() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        TransactionTemplateService transactionTemplateService = applicationContext.getBean("transactionTemplateService", TransactionTemplateService.class);
        transactionTemplateService.test();
    }

    @Test
    public void testSellService() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        SellService sellService = applicationContext.getBean("sellService", SellService.class);
        // sellService.sellProduct();
        sellService.sellProduc_1t();
    }



    @Test
    public void testSelect() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        RepeatAbleService repeatAbleService = applicationContext.getBean("repeatAbleService", RepeatAbleService.class);
        repeatAbleService.select();
    }

    @Test
    public void testUpdate() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        RepeatAbleService repeatAbleService = applicationContext.getBean("repeatAbleService", RepeatAbleService.class);
        repeatAbleService.update();
    }
}
